package cn.yuanqiao.archive.wenjianzhengli.utils.convert;

import cn.hutool.core.util.IdUtil;
import cn.hutool.system.SystemUtil;
import org.springframework.util.FileCopyUtils;
import org.springframework.util.StringUtils;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.UUID;

public abstract class MediaFormatConvert implements FileFormatConvert {

    protected void close(InputStream input) {
        try {
            input.close();
        } catch (IOException ignored) {
        }
    }

    protected void close(OutputStream output) {
        if (output != null) {
            try {
                output.close();
            } catch (IOException ignored) {
            }
        }
    }

    protected String getTempDir() {
        return SystemUtil.getUserInfo().getTempDir();
    }

    protected Path getTempPath(String suffix) {
        return Paths.get(getTempDir(), UUID.randomUUID() + "." + suffix);
    }

    protected void delete(Path path) {
        try {
            if (Files.exists(path)) {
                Files.delete(path);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void convert(UUID uuid, InputStream input, String inputFormat, OutputStream output, String outputFormat) throws Exception {
        final Path tmpInputPath = getTempPath(inputFormat);
        final Path tmpOutputPath = getTempPath(outputFormat);
        try {
            FileCopyUtils.copy(input, Files.newOutputStream(tmpInputPath));
            convert(uuid, tmpInputPath, tmpOutputPath);
            FileCopyUtils.copy(Files.newInputStream(tmpOutputPath), output);
        } finally {
            delete(tmpOutputPath);
            delete(tmpInputPath);
        }
    }

    /*
     * 转换备份原始文件
     * */
    public Path convertBakRawFile(UUID uuid, Path input, String inputFormat, Path output, String outputFormat) throws Exception {
        final Path rawFilePath = getRawFilePath(input);
        // copy 是防止文件损坏
        FileCopyUtils.copy(input.toFile(), rawFilePath.toFile());
        //Files.copy(input, rawFilePath, StandardCopyOption.REPLACE_EXISTING);
        convert(uuid, rawFilePath, inputFormat, output, outputFormat);
        return rawFilePath;
    }

    public Path convertBakRawFile(UUID uuid, Path input, Path output) throws Exception {
        final Path rawFilePath = getRawFilePath(input);
        // copy 是防止文件损坏
        final int copy = FileCopyUtils.copy(input.toFile(), rawFilePath.toFile());
        //Files.copy(input, rawFilePath, StandardCopyOption.REPLACE_EXISTING);
        convert(uuid, rawFilePath, output);
        return rawFilePath;
    }

    /*
     * 转换可查看的格式 mp3 mp4 png jpg 等
     * */
    public abstract Path convertViewFile(UUID uuid,Path input) throws Exception;

    public abstract Path convertThumbnailFile(Path input, int w, int h) throws Exception;

    /*
     * 转换一次存一次原始文件路径
     * */
    public Path getRawFilePath(Path path) throws IOException {
        // 原始文件存放
        final String filename = path.toFile().getName();
        final Path rawfile = path.getParent().resolve("rawfile");
        if (Files.notExists(rawfile)) {
            Files.createDirectories(rawfile);
        }
        return rawfile.resolve(IdUtil.fastSimpleUUID() + "_" + filename);
    }

    /*
     * 转换给可预览的文件路径
     * */
    public Path getViewFilePath(Path path) throws IOException {
        // 原始文件存放
        final String filename = StringUtils.stripFilenameExtension(path.toFile().getName());
        final Path viewfile = path.getParent().resolve("viewfile");
        if (Files.notExists(viewfile)) {
            Files.createDirectories(viewfile);
        }
        return viewfile.resolve(IdUtil.fastSimpleUUID() + "_" + filename);
    }

    /*
     * 获取缩略图路径
     * */
    public Path getThumbnailPath(Path path) throws IOException {
        // 原始文件存放
        final String filename = path.toFile().getName();
        final Path thumbnail = path.getParent().resolve("thumbnailfile");
        if (Files.notExists(thumbnail)) {
            Files.createDirectories(thumbnail);
        }
        return thumbnail.resolve(IdUtil.fastSimpleUUID() + "_" + filename.concat(".jpg"));
    }
}
